<template>
<view class="move-container">
    <view class="form-section">
      <view class="form-item">
        <text class="label">地码
          <text class="required">*</text>
        </text>
        <input
            v-model="formData.fromLocation"
            class="input"
            type="text"
            placeholder="请扫描或输入地码"
        />
      </view>
      <view class="four-btn-group-2row">
        <view class="btn-row">
          <button class="big-blue-btn" @click="handleMaterial">物料补充</button>
          <button class="big-blue-btn" @click="handleTray">回收托盘</button>
        </view>
        <view class="btn-row">
          <button class="big-blue-btn" @click="handleGround">地面入库</button>
          <button class="big-blue-btn" @click="handleContainer">母器具补货</button>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
	
import {moveStock} from '@/api/system/stock';

var main, receiver, filter;

export default {
  data() {
    return {
		billOptions: [
        {value: 'bill1', label: '入库单001'},
        {value: 'bill2', label: '入库单002'},
        {value: 'bill3', label: '入库单003'}
      ],
	   billIndex: 0,
      formData: {
        fromLocation: '',
        toLocation: '',
      /*  materialCode: '',
        quantity: '',*/
        remark: ''
      },
      loading: false
    };
  },

  onLoad() {
    this.initScan();
  },

  onShow() {
    this.startScan();
  },

  onHide() {
    this.stopScan();
  },

  onUnload() {
    this.stopScan();
  },

  methods: {
	   onBillChange(e) {
	        this.billIndex = e.detail.value;
	        // 更新表单数据
	        this.formData.instrock = this.billOptions[this.billIndex].value;
	      },
    initScan() {
      let _this = this;
      /* #ifdef APP-PLUS */
      main = plus.android.runtimeMainActivity();
      var IntentFilter = plus.android.importClass('android.content.IntentFilter');
      filter = new IntentFilter();
      filter.addAction("com.service.scanner.data")
      receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
        onReceive: function (context, intent) {
          plus.android.importClass(intent);
          let code = intent.getStringExtra("ScanCode");
          console.log(code)
          _this.handleScanResult(code);
        }
      });
      /* #endif */
    },

    startScan() {
      /* #ifdef APP-PLUS */
      main.registerReceiver(receiver, filter);
      /* #endif */
    },

    stopScan() {
      /* #ifdef APP-PLUS */
      main.unregisterReceiver(receiver);
      /* #endif */
    },

    // 处理扫码结果
    handleScanResult(code) {
      // 判断扫码内容类型
      if (code.startsWith('L')) {
        // 库位格式
        if (!this.formData.fromLocation) {
          this.formData.fromLocation = code.substring(1);
        } else if (!this.formData.toLocation) {
          this.formData.toLocation = code.substring(1);
        }
      } else if (code.startsWith('M')) {
        // 物料编号格式
        this.formData.materialCode = code.substring(1);
      } else {
        // 默认当作物料编号处理
        this.formData.materialCode = code;
      }
    },

    // 重置表单
    handleReset() {
      this.formData = {
        fromLocation: '',
        toLocation: '',
       /* materialCode: '',
        quantity: '',*/
        remark: ''
      };
    },

    // 提交表单
    handleSubmit() {
      if (this.loading) return;

      // 表单验证
      if (!this.formData.fromLocation) {
        this.$modal.msgError('请输入起点位置');
        return;
      }
      if (!this.formData.toLocation) {
        this.$modal.msgError('请输入终点位置');
        return;
      }
      /*if (!this.formData.materialCode) {
        this.$modal.msgError('请输入物料编号');
        return;
      }
      if (!this.formData.quantity) {
        this.$modal.msgError('请输入移动数量');
        return;
      }*/

      this.loading = true;
      moveStock(this.formData)
          .then(res => {
			  console.log(data,"data")
            if (res.code === 200) {
              this.$modal.msgSuccess('移动成功');
              this.handleReset();
            } else {
              this.$modal.msgError(res.msg || '移动失败');
            }
          })
          .catch(error => {
            console.error('库存移动失败:', error);
            this.$modal.msgError('移动失败，请稍后重试');
          })
          .finally(() => {
            this.loading = false;
          });
    },

    // 以下为四个按钮的模拟请求
    handleMaterial() {
      // 模拟物料补充接口
      this.$modal.msgSuccess('请求物料补充接口: /api/material');
    },
    handleTray() {
      // 模拟回收托盘接口
      this.$modal.msgSuccess('请求回收托盘接口: /api/tray');
    },
    handleGround() {
      // 模拟地面入库接口
      this.$modal.msgSuccess('请求地面入库接口: /api/ground');
    },
    handleContainer() {
      // 模拟母器具补货接口
      this.$modal.msgSuccess('请求母器具补货接口: /api/container');
    },
  }
}
</script>

<style lang="scss" scoped>
	/* 样式保持不变 */
	.move-container {
	  min-height: 100vh;
	  background-color: #f8f9fa;
	  padding: 16px;
	}
.move-container {
  min-height: 100vh;
  background-color: #f8f9fa;
  padding: 16px;
}

.form-section {
  background-color: #fff;
  border-radius: 8px;
  padding: 32px 16px 32px 16px;
  max-width: 1200px;
  margin: 40px auto 0 auto;
}

.form-item {
  margin-bottom: 32px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}

.label {
  font-size: 22px;
  color: #222;
  font-weight: 700;
  margin-bottom: 12px;
  letter-spacing: 2px;
}

.required {
  color: #ff4d4f;
  margin-left: 4px;
  font-size: 18px;
}

.input {
  width: 100%;
  min-width: 0;
  height: 48px;
  border: none;
  outline: none;
  border-radius: 8px;
  background: #f3f4f6;
  font-size: 20px;
  color: #222;
  padding: 0 20px;
  box-sizing: border-box;
  transition: box-shadow 0.2s;
  box-shadow: 0 1px 2px rgba(0,0,0,0.03);
}

.input::placeholder {
  color: #b0b0b0;
  font-size: 18px;
}

.four-btn-group-2row {
  display: flex;
  flex-direction: column;
  gap: 32px;
  margin-top: 32px;
}
.btn-row {
  display: flex;
  flex-direction: row;
  gap: 48px;
  justify-content: flex-start;
}
.big-blue-btn {
  width: 300px;
  height: 90px;
  background: #2686fa;
  color: #fff;
  border: none;
  border-radius: 10px;
  font-size: 36px;
  font-weight: 700;
  letter-spacing: 2px;
  transition: background 0.2s;
  box-shadow: 0 2px 8px 0 rgba(38,134,250,0.08);
}
.big-blue-btn:active {
  background: #1566c2;
}
@media (max-width: 1300px) {
  .btn-row {
    gap: 24px;
  }
  .big-blue-btn {
    width: 180px;
    height: 60px;
    font-size: 22px;
  }
  .input {
    font-size: 16px;
    height: 36px;
    padding: 0 10px;
  }
}
@media (max-width: 700px) {
  .four-btn-group-2row {
    gap: 16px;
  }
  .btn-row {
    gap: 12px;
  }
  .big-blue-btn {
    width: 100vw;
    min-width: 120px;
    max-width: 100%;
    font-size: 18px;
    height: 44px;
  }
  .input {
    font-size: 15px;
    height: 32px;
    padding: 0 6px;
  }
}
</style>